//index.js
const app = getApp()
const api = app.globalData.api
const rule={
  page2Data:0,
  page3Data:1,
  page4Data:2,
  page5Data:3
}
Page({
  onShareAppMessage: function () {
    return {
      title: app.globalData.app,
      path: '/pages/index/index'
    }
  },
  data: {
    //一级导航菜单
    navtitle: [{name:'首页',id:1},{name:'客照',id:2},{name:'样照',id:3},{name:'活动',id:4},{name:'套系',id:5}],
    //导航位置
    nowNav:1,
    //page2二级分类
    ClassType: null,
    //二级分类位置
    nowType:null,
    //首页数据
    indexData:{
      swiper: [],
      slide: { 
        title: '套系价格',
        content: [
          {
            name:'标题',
            src:''
          },
          {
            name: '标题',
            src: ''
          },
          {
            name: '标题',
            src: ''
          }
        ] 
      },
      blockList: {
        title: '最新样片',
        content: [
            {
              name:'标题',
              src:''
            },
            {
              name: '标题',
              src: ''
            },
        ] 
      },
      blockTabList: {
        title: '最新客片',
        content: [
          {
            name:'标题',
            src:''
          }
        ] 
      },
    },
    //page2页数据
    page2Data:[],
    //page3页数据
    page3Data:[],
    //page4页数据
    page4Data:[],
    //page5页数据
    page5Data:[],
    //同步锁
    lock:true,
    srcApi:'',
    //滚动位置
    scroll:0,
    appid:'',
    //是否显示up按钮
    upShow:false
  },
  onLoad: function () {
    let _this = this
    this.data.appid=app.globalData.appid
    //初始化app名称
    wx.setNavigationBarTitle({
      title: app.globalData.app
    })
    this.setData({
      srcApi: app.globalData.srcApi,
    })
    
    wx.showLoading({
      title: '数据加载中',
      mask:true
    })
    //首页数据初始化
    wx.request({
      url: api + '/get_index?appid=' + this.data.appid + '&session_key=' + app.globalData.key,
      success: function (res) {
        let data = res.data.data
        if (data) {
          _this.setData({
            indexData:data
          })
        }
      },
      fail:function(res){
        wx.showModal({
          title: '请检查网络',
          content: res.errMsg
        })
      },
      complete:function(){
        wx.hideLoading()
      }
    })
    
    //导航菜单
    wx.request({
      url: api + '/nav?appid=' + this.data.appid + '&session_key=' + app.globalData.key,
      success:function(res){
        let data=res.data.data
        if(data){
          _this.setData({
            navtitle: data.nav,
            nowNav: data.nav[0].id
          })
          app.globalData.route=data.nav[0].from
        }
      },
      fail:function(){
        wx.showToast({
          title: '加载失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
    
  },
  //导航菜单点击事件
  navClicked: function (e) {
    let _this=this
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    this.setData({
      nowNav: id,
    })
    if (_this.data.navtitle[index].from){
      app.globalData.route = _this.data.navtitle[index].from
    }
    //当点击page2时
    if (this.data.nowNav === this.data.navtitle[2].id){
      if (!this.data.page3Data.length)
      {
        wx.request({
          url: api + '/sample/?appid=' + _this.data.appid + '&session_key=' + app.globalData.key,
          success: function (res) {
            let data=res.data.data
            if(data){
              _this.setData({
                ClassType: data,
                nowType: data[0].temp_mod_id
              })
              
              _this.dataGet(data[0].temp_mod_id, 0, 6,function(data){
                let pageData = _this.data.page3Data
                _this.setData({
                  page3Data: pageData.concat(data.content)
                })
              })
            }
          }
        })
      }
    }else if(this.data.nowNav===this.data.navtitle[1].id){
      if (!this.data.page2Data.length) {
        _this.dataGet(this.data.navtitle[1].id, 0, 6, function (data) {
          let pageData = _this.data.page2Data
          _this.setData({
            page2Data: pageData.concat(data.content)
          })
        })
      }
    }else if (this.data.nowNav === this.data.navtitle[3].id) {
      if (!this.data.page4Data.length){
        _this.dataGet(this.data.navtitle[3].id, 0, 6, function (data) {
            let pageData = _this.data.page4Data
            _this.setData({
              page4Data: pageData.concat(data.content)
            })
          })
        }
    }else if(this.data.nowNav===this.data.navtitle[4].id){
      if (!this.data.page5Data.length){
        _this.dataGet(this.data.navtitle[4].id,0,6, function (data) {
          let pageData=_this.data.page5Data
          _this.setData({
            page5Data: pageData.concat(data.content)
          })
        })
      }
    }
  },
  //数据请求
  dataGet: function (modId, offset, limit,callback){
    let _this=this
    wx.request({
      url: api + '/list/?temp_mod_id=' + modId + '&appid=' + _this.data.appid + '&offset=' + offset + '&limit=' + limit + '&session_key=' + app.globalData.key,
      success:function(res){
        let data=res.data.data
        callback(data)
      },
      fail:function(){
        wx.showToast({
          title: '加载失败',
          icon:'none',
          duration: 2000
        })
      }
    })
  },
  //滑动置底触发事件
  tolowerPage:function(e){
    let _this=this
    let key = e.currentTarget.dataset.key
      if (this.data.lock) {
        this.data.lock = false
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        setTimeout(function () {
          let id = _this.data.nowNav
          if (id === _this.data.navtitle[2].id){
            id=_this.data.nowType
          }
          
          _this.dataGet(id, _this.data[key].length, 6, function (data) {
            let pageData = _this.data[key]
            wx.hideLoading()
            if (pageData.length == data.sum) {
              
              wx.showToast({
                title: '没有更多了',
                icon: 'none',
                duration: 1000
              })
            }
            if (data.content) {
              pageData = pageData.concat(data.content)
              _this.setData({
                [key]: pageData
              })
            }
          })
          _this.data.lock = true
        }, 1000)
      }
    
  },
  //滚动置顶
  up:function(){
    this.setData({
      scroll:0
    })
  },
  //子组件触发事件改变nowType值
  nowTypeChange:function(e){
    let _this=this
    this.dataGet(e.detail,0,6,function(res){
      if(res.content){
        _this.setData({
          page3Data:res.content
        })
      }else{
        _this.setData({
          page3Data:[]
        })
      }
    })
  },
  //判断滚动位置
  upper:function(e){
    if (e.detail.scrollTop>750){
      this.setData({
        upShow:true
      })
    }else{
      this.setData({
        upShow:false
      })
    }
  }
})
